
var page_index=["index_page"];
var newcar_page=1;
var body_class=['login_third_step_page','login_second_step_page','login_first_step_page','help_page','company_profile_page','search_page','message_page','pay_record_page','baoxian_page','baoyang_page'];
$(function(){ 
    getIndexData()
    bindEvent_index();  
    function getIndexData(type){   
        var spinner = new Spinner();     
    　　$.ajax({
            type: "POST",
            url: webConfig.url+"/Home/Index/getIndexData",
            data: {},
            dataType: "JSONP",
            crossDomain: true,
            beforeSend:function(){
                // $('#loadingBox').removeClass('hidden')
                // var target = document.getElementById('loadingBox');
                // spinner.spin(target)
            },
            complete:function(){
                // $('#loadingBox').addClass('hidden')
                // spinner.spin()
            },
            success: function(data){
                var promo1=''
                var promo2=''
                var promo3=''
                var promo4=''
                if(data['promo_car']){
                    for(var i in data['promo_car']){
                        var title=''
                        var first_class="";
                        title="极速提车"
                        
                        var name=data['promo_car'][i]['name']
                        var carnum=data['promo_car'][i]['carnum']
                        var details=data['promo_car'][i]['details']
                        var guide_price=getMoneyText(data['promo_car'][i]['guide_price'])
                        var jdc_price=getMoneyText(data['promo_car'][i]['jdc_price'])
                        var first_pay=getMoneyText(data['promo_car'][i]['first_pay'])
                        var month_pay=parseInt(data['promo_car'][i]['month_pay'])
                        var url=webConfig.ossUrl+"/Image/Cars/"+data['promo_car'][i]['carnum']+"/1.jpg"

                        if(i==0){
                            var details_arr=details.split(' ');
                            var details_str='';
                            for(var d in details_arr){
                                if(d<2){
                                    details_str+='<span class="no_first_pay_car_info no_first_span">'+details_arr[d]+'</span>';
                                }
                            }
                            promo1='<a style="position: relative">\
                                    <div class="no_first_pay_title no_first_title">\
                                        <span>'+title+'</span>\
                                    </div>\
                                    <div class="no_first_pay_content car_info car_info1" carnum="'+carnum+'">\
                                        <div class="no_first_pay_title_left">\
                                            <div class="no_first_pay_title_left_content">\
                                                <div style="margin: 10px 0 5px 0;">\
                                                    <span class="no_first_pay_car_name">'+name+'</span>\
                                                </div>\
                                                <div class="div_widtn">\
                                                    '+details_str+'\
                                                </div>\
                                                <div>\
                                                    <span class="zhidao_ccc fwf">服务费'+first_pay+'</span>\
                                                    <span class="no_first_pay_car_now_pay">月租金'+month_pay+'元</span>\
                                                </div>\
                                            </div>\
                                        </div>\
                                        <div class="no_first_pay_title_right img_right">\
                                            <img src="'+url+'">\
                                        </div>\
                                        <div class="clearfix"></div>\
                                    </div>\
                                </a>'
                        }else {
                            var promo_str='<div class="half_first_pay_part mragin_div car_info" carnum="'+carnum+'">\
                                                <a>\
                                                    <div class="half_first_pay_content">\
                                                        <img src="'+url+'">\
                                                        <span style="font-weight: 700">'+name+'</span>\
                                                        <span class="month_red">筋斗价'+jdc_price+'</span>\
                                                        <span class="zhidao_ccc">指导价'+guide_price+'</span>\
                                                    </div>\
                                                </a>\
                                                <img src="images/jisu.png" alt="" class="jisu_img">\
                                            </div>'
                            if(i>0 && i<=3){
                                promo2+=promo_str
                            }else if(i>3 && i<=6){
                                promo3+=promo_str
                            }else{
                                promo4+=promo_str
                            }

                        }
                    }
                }
                var new_car=""
                if(data['new_car']){
                    
                    for(var i in data['new_car']){
                        var name=data['new_car'][i]['name']
                        var carnum=data['new_car'][i]['carnum']
                        var details=data['new_car'][i]['details']
                        var guide_price=getMoneyText(data['new_car'][i]['guide_price'])
                        var jdc_price=getMoneyText(data['new_car'][i]['jdc_price'])
                        var first_pay=getMoneyText(data['new_car'][i]['first_pay'])
                        var month_pay=parseInt(data['new_car'][i]['month_pay'])
                        var url=webConfig.ossUrl+"/Image/Cars/"+data['new_car'][i]['carnum']+"/1.jpg"
                        var details_arr=details.split(' ');
                        var details_str='';
                        for(var d in details_arr){
                            if(d<2){
                                details_str+='<span class="no_first_pay_car_info no_first_span">'+details_arr[d]+'</span>';
                            }
                        }
                        new_car+='<div class="index_new_car_option car_info" carnum="'+carnum+'">\
                                    <a>\
                                        <img src="'+url+'">\
                                        <div class="pt_left pt_div">\
                                            <span class="no_first_pay_car_xname">'+name+'</span>\
                                        </div>\
                                        <div class="pt_left xdiv_widtn">\
                                        '+details_str+'\
                                        </div>\
                                        <div class="pt_left">\
                                            <span class="no_first_pay_car_now_pay month_redx">筋斗价'+jdc_price+'</span>\
                                            <span class="no_first_pay_car_former_pay new_former_pay zhidao_ccc">指导价'+guide_price+'</span>\
                                        </div>\
                                    </a>\
                                </div>'
                    }
                    
                }
                var hot_car=""
                if(data['hot_car']){
                    
                    for(var i in data['hot_car']){
                        var name=data['hot_car'][i]['name']
                        var carnum=data['hot_car'][i]['carnum']
                        var details=data['hot_car'][i]['details']
                        var guide_price=getMoneyText(data['hot_car'][i]['guide_price'])
                        var jdc_price=getMoneyText(data['hot_car'][i]['jdc_price'])
                        var first_pay=getMoneyText(data['hot_car'][i]['first_pay'])
                        var month_pay=parseInt(data['hot_car'][i]['month_pay'])
                        var desc=data['hot_car'][i]['desc']
                        var url=webConfig.ossUrl+"/Image/Cars/"+data['hot_car'][i]['carnum']+"/1.jpg"
                        var details_arr=details.split(' ');
                        var details_str='';
                        for(var d in details_arr){
                            if(d<2){
                                details_str+='<span class="no_first_pay_car_info no_first_span">'+details_arr[d]+'</span>';
                            }
                            
                        }
                        hot_car+='<div class="index_new_car_option car_info" carnum="'+carnum+'">\
                                    <a>\
                                        <img src="'+url+'">\
                                        <div class="pt_left pt_div">\
                                            <span class="no_first_pay_car_xname">'+name+'</span>\
                                            <span class="new_car_flagtip">'+desc+'</span>\
                                        </div>\
                                        <div class="pt_left xdiv_widtn">\
                                            '+details_str+'\
                                        </div>\
                                        <div class="pt_left">\
                                            <span class="no_first_pay_car_now_pay month_redx">筋斗价'+jdc_price+'</span>\
                                            <span class="no_first_pay_car_former_pay new_former_pay zhidao_ccc">指导价'+guide_price+'</span>\
                                        </div>\
                                    </a>\
                                </div>'
                    }
                    
                }
                var content='<div class="index_body_content">\
                                    <div class="swiper-container index_top_slide" id="index_top_slide">\
                                        <div class="swiper-wrapper">\
                                            <div class="swiper-slide">\
                                                <a><img src="'+webConfig.ossUrl+'/Image/Title/1.jpg"></a>\
                                            </div>\
                                            <div class="swiper-slide">\
                                                <a><img src="'+webConfig.ossUrl+'/Image/Title/2.jpg"></a>\
                                            </div>\
                                            <div class="swiper-slide">\
                                                <a><img src="'+webConfig.ossUrl+'/Image/Title/3.jpg"></a>\
                                            </div>\
                                        </div>\
                                        <!-- 如果需要分页器 -->\
                                        <div class="swiper-pagination"></div>\
                                    </div>\
                              </div>\
                            <!--导航-->\
                            <div class="index_new_car">\
                                <ul class="car_ul">\
                                    <li class="select_first_pay">1万以下</li>\
                                    <li class="select_first_pay">1-2万</li>\
                                    <li class="select_first_pay">2-3万</li>\
                                    <li class="select_first_pay">3-4万</li>\
                                    <li class="select_first_pay">4-5万</li>\
                                </ul>\
                                <div class="index_new_car_brand">\
                                    <div class="new_car_brand">\
                                        <a class="car_img_span select_brand">\
                                            <img src="images/brand-41.png">\
                                            <span>大众</span>\
                                        </a>\
                                    </div>\
                                    <div class="new_car_brand">\
                                        <a class="car_img_span select_brand">\
                                            <img src="images/brand-30.png">\
                                            <span>别克</span>\
                                        </a></div>\
                                    <div class="new_car_brand">\
                                        <a class="car_img_span select_brand">\
                                            <img src="images/brand-121.png">\
                                            <span>日产</span>\
                                        </a>\
                                    </div>\
                                    <div class="new_car_brand">\
                                        <a class="car_img_span select_brand">\
                                            <img src="images/brand-27.png">\
                                            <span>本田</span>\
                                        </a>\
                                    </div>\
                                    <div class="new_car_brand">\
                                        <a class="car_img_span select_brand">\
                                            <img src="images/brand-53.png">\
                                            <span>福特</span>\
                                        </a>\
                                    </div>\
                                    <div class="clearfix"></div>\
                                </div>\
                            </div>\
                            <!--分割线-->\
                            <div class="fg"></div>\
                            <!--主体内容 极速提车-->\
                            <div class="swiper-container first_pay_slide" id="first_pay_slide">\
                              <div class="swiper-wrapper">\
                               <div class="swiper-slide swiper_height">\
                                <div class="no_first_pay">\
                                    '+promo1+'\
                                    <div class="half_first_pay">\
                                        '+promo2+'\
                                        <div class="clearfix"></div>\
                                    </div>\
                                 </div>\
                               </div>\
                               <div class="swiper-slide swiper_height swiper_margin_k">\
                                    <div class="half_first_pay">\
                                        '+promo3+'\
                                        <div class="clearfix"></div>\
                                    </div>\
                                    <div class="half_first_pay">\
                                        '+promo4+'\
                                        <div class="clearfix"></div>\
                                    </div>\
                                </div>\
                              </div>\
                                <!-- 如果需要分页器 -->\
                                <div class="swiper-pagination swiper-pagination-clickable swiper-pagination-bullets swiper-promo">\
                                </div>\
                           </div>\
                            <!--分割线-->\
                            <div class="fg"></div>\
                            <!--新品-->\
                            <div class="new_product">\
                                <div class="index_new_car_title">\
                                    <span>新品上架</span>\
                                </div>\
                                <div class="index_new_car_container">\
                                    '+new_car+'\
                                    <div class="clearfix"></div>\
                                </div>\
                            </div>\
                            <!--分割线-->\
                            <div class="fg"></div>\
                            <!--热销-->\
                            <div class="new_product">\
                                <div class="index_new_car_title">\
                                    <span>新车热销</span>\
                                </div>\
                                <div class="index_new_car_container">\
                                    '+hot_car+'\
                                    <div class="clearfix"></div>\
                                </div>\
                            </div>\
                            <!--分割线-->\
                            <div class="fg"></div>\
                            <!--筋斗问答-->\
                            <div class="new_product" style="margin-bottom: 50px">\
                                <div class="index_new_car_title question_title_border">\
                                    <span>筋斗车问答</span>\
                                </div>\
                                <div class="question_answer">\
                                    <div class="question_answer_container">\
                                        <div class="index_question index_question_info" index="1">\
                                            <div class="index_question_dot">\
                                                <span></span>\
                                            </div>\
                                            <span class="index_question_words">筋斗车的购车流程是怎样的呢？</span>\
                                            <i class="mui_i mui-icon mui-icon-arrowdown"></i>\
                                            <div class="clearfix"></div>\
                                        </div>\
                                        <div class="index_answer index_answer_info hidden padding_l" index="1" style="display: none;">\
                                            <p>筋斗车购车一共分为四步：接受我司地面团队审核、前往车库看车、支付综合费用、提车。</p>\
                                            <p>1.地面团队审核:通过APP联系我司区域顾问，顾问详解购车内容，同意后提交客户审核资料；</p>\
                                            <p>2.前往车库看车:审核通过以后，前往我司指定车库（设于省会城市），选择心仪车辆；</p>\
                                            <p>3.支付综合费用:签署相关协议并支付综合费用；</p>\
                                            <p>4.提车:客户在车库将心仪车辆提走。</p>\
                                        </div>\
                                    </div>\
                                </div>\
                                <div class="question_answer">\
                                    <div class="question_answer_container">\
                                        <div class="index_question index_question_info car_detail" index="2">\
                                            <div class="index_question_dot">\
                                                <span></span>\
                                            </div>\
                                            <span class="index_question_words">筋斗车的申请租车流程怎样的呢？</span>\
                                            <i class="mui-icon mui-icon-arrowdown mui_i"></i>\
                                            <div class="clearfix"></div>\
                                        </div>\
                                        <div class="index_answer index_answer_info hidden padding_l" index="2" style="display: none;">\
                                            <p>1.客户下载筋斗车APP；</p>\
                                            <p>2.当地服务顾问与客户面聊服务细则；</p>\
                                            <p>3.当地服务顾问对客户进行资质评定并做线上提交申请；</p>\
                                            <p>4.审核通过后客户前往车库选择车辆；</p>\
                                            <p>5.办理签约手续及支付综合费用；</p>\
                                            <p>6.等待筋斗车上牌及保险生效；</p>\
                                            <p>7.提车；</p>\
                                        </div>\
                                    </div>\
                                </div>\
                                <div class="question_answer_more mine_help">\
                                    <a>查看更多</a>\
                                </div>'
                $('.index_page_content').html(content)
                var mySwiper = new Swiper ('#index_top_slide', {
                    loop: true,
                    autoplay : 3000,
                    // 如果需要分页器
                    pagination: '.swiper-pagination',
                    paginationClickable :true

                });
                var mySwiper = new Swiper ('#first_pay_slide', {
                    // 如果需要分页器
                    pagination: '.swiper-pagination',
                    paginationClickable :true

                });
                var mySwiper = new Swiper ('#car_owner_story', {
                    slidesPerView : 1.3,
                    spaceBetween : 10
                }); 
                $("body").delegate(".index_question_info","click",function(){
                    var index=$(this).attr('index');
                    $(".index_answer_info[index="+index+"]").slideToggle();
                    $(this).find(".fa").toggleClass("fa-angle-down");
                    $(this).find(".fa").toggleClass("fa-angle-up");
                });
                mui('.mui-scroll-wrapper').scroll({
                    deceleration: 0.005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006 
                });
            },
            error:function(){
                var content='<div class="service_no_car_tip index_no_service">\
                                <img src="images/off_the_net.jpg">\
                                <span>网络出错了</span>\
                                <div class="go_buy_car_container">\
                                    <a class="go_buy_car refuse_index">\
                                        刷新\
                                    </a>\
                                </div>\
                            </div>'
                $('.index_page_content').html(content)  
            }
        });
        
    }

    
    
    function bindEvent_index() {
        
        //显示首页
        //首页锚点设置
        $("body").delegate(".index_click","click",function(){
            $("#"+page_index[0]).hide();
            $("#index_page").show();
            page_index[0]="index_page";     
        });
        
        $("body").delegate(".newcar_click","click",function(){
            //追加显示新车页面
            if(!$('#newcar_page').length>0){
                $("body").append(html_content.newcar);
                bindEvent_newcar()
                bindPull_newcar()
                getNewCarData();
            }
            $("#"+page_index[0]).hide();
            $("#newcar_page").show();
            page_index[0]="newcar_page"; 
        });
        
        $("body").delegate(".service_click","click",function(){
            //追加显示服务页面 
            if(!$('#service_page').length>0){
                $("body").append(html_content.service);
                bindEvent_service()
                bindPull_service()
                getUserOrder()
            }
            $("#"+page_index[0]).hide();
            $("#service_page").show();
            page_index[0]="service_page";
        });
        
         $("body").delegate(".mine_click","click",function(){
            //追加显示我的页面
            if(!$('#mine_page').length>0){
                $("body").append(html_content.mine);
                bindEvent_mine()
            }
            $("#"+page_index[0]).hide();
            $("#mine_page").show();
            page_index[0]="mine_page";
        });

        $("body").delegate(".car_info","click",function(){
            if($('#car_detail_page').length>0){
               $('#car_detail_page').remove() 
            }
            $("body").append(html_content.car_detail);
            $("#"+page_index[page_index.length-1]).hide();
            $("#car_detail_page").show();
            page_index.push("car_detail_page");
            var carnum=$(this).attr('carnum')
            getDetailDate(carnum)
        })
        $("body").delegate(".back_page","click",function(){
            $("#"+page_index[page_index.length-1]).hide();
            page_index.splice(page_index.length-1,1)
            $("#"+page_index[page_index.length-1]).show(); 
            if(!isInArray(body_class,page_index[page_index.length-1])){
                $('body').removeAttr('class') 
            } 
                    
        });

        $("body").delegate(".select_first_pay","click",function(){
            resetNewCarSelect()
            //追加显示新车页面
            if(!$('#newcar_page').length>0){
                $("body").append(html_content.newcar);
                bindEvent_newcar()
                bindPull_newcar()
            }
            var value=$(this).index()+1;
            var text=$('.hidden_first_pay_option:eq('+value+')').find('span').text();
            $('#newcar_select_2').find('span').text(text);
            $('#newcar_select_2').attr('value',value)
            $('.hidden_first_pay_option:eq('+value+')').siblings().removeClass("active");
            $('.hidden_first_pay_option:eq('+value+')').addClass("active")
            newcar_page=1
            getNewCarData();
            $("#"+page_index[0]).hide();
            $("#newcar_page").show();
            page_index[0]="newcar_page"; 
        });
        $("body").delegate(".select_brand","click",function(){
            resetNewCarSelect()
            //追加显示新车页面
            if(!$('#newcar_page').length>0){
                $("body").append(html_content.newcar);
                bindEvent_newcar()
                bindPull_newcar()
            }
            var brand=$(this).find('span').text();
            $("#newcar_select_brand span").text(brand);
            newcar_page=1
            getNewCarData();
            $("#"+page_index[0]).hide();
            $("#newcar_page").show();
            page_index[0]="newcar_page"; 
        });

        $("body").delegate(".refuse_index","click",function(){
            getIndexData();
        })
        $("body").delegate(".search_index","click",function(){
            if(!$('#search_page').length>0){
                $("body").append(html_content.search);
                bindEvent_search()
            }
            $("#"+page_index[page_index.length-1]).hide();
            $("#search_page").show();
            page_index.push("search_page");
            $('body').attr('class','search_body')
            begin_search()
        })
        $("body").delegate(".message_index","click",function(){
            if(!$('#message_page').length>0){
                $("body").append(html_content.message);
                bindEvent_message()
            }
            $("#"+page_index[page_index.length-1]).hide();
            $("#message_page").show();
            page_index.push("message_page");
            $('body').attr('class','search_body')
        })
         
    }
    
    
}); 
function getMoneyText(number){
    number=(parseInt(number)/10000).toFixed(2)+"万"
    return number;
}
function isInArray(arr,value){
    for(var i = 0; i < arr.length; i++){
        if(value === arr[i]){
            return true;
        }
    }
    return false;
}
